package com.hulytu.android.adapter.listview;

import android.util.SparseArray;
import android.view.View;


import com.hulytu.android.adapter.ViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class BasicViewHolder implements ViewHolder {

    private final SparseArray<View> cacheViews = new SparseArray<>(8);

    @NotNull
    private final View itemView;

    private int adapterPosition = NO_POSITION;

    public BasicViewHolder(@NotNull View itemView) {
        this.itemView = Objects.requireNonNull(itemView);
    }

    @NotNull
    @Override
    public View getItemView() {
        return itemView;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <V extends View> V findViewById(int viewId) {

        View cacheView = cacheViews.get(viewId);

        if (cacheView == null) {
            V view = itemView.findViewById(viewId);
            cacheViews.put(viewId, view);
            return view;
        }

        return (V) cacheView;
    }

    @Override
    public int getAdapterPosition() {
        return adapterPosition;
    }

    public void setAdapterPosition(int adapterPosition) {
        this.adapterPosition = adapterPosition;
    }
}
